home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / Aidan's Class Libraries / Source / Miscellany / StrStuff.cpp
Encoding:
Text File  |  1997-05-18  |  1.6 KB  |  81 lines  |  [TEXT/CWIE]

  1. //Copyright (c) 1997 Aidan Cully
  2. //All rights reserved
  3.  
  4. #include "CLStrStuff.h"
  5.  
  6. unsigned char *StrCpy( unsigned char *dest, const unsigned char *src ) {
  7.     unsigned char *origDest = dest;
  8.     unsigned char count = *src+1;
  9.  
  10.     while( count-- )
  11.         *dest++ = *src++;
  12.     return( origDest );
  13. }
  14.  
  15. SInt8 StrCmp( const unsigned char *str1, const unsigned char *str2 ) {
  16.     int index, count;
  17.     Boolean equalLen;
  18.  
  19.     equalLen = *str1 == *str2;
  20.     count = *str1 < *str2 ? *str1 : *str2;
  21.     str1++;
  22.     str2++;
  23.     index = 2l;
  24.     while( (*str1++ == *str2++) && (index < count ) )
  25.         index++;
  26.     if( (index == count) && equalLen && (*str1 == *str2) )
  27.         return( 0 );
  28.     return( 1 );
  29. }
  30.  
  31. unsigned char *IToA( SInt16 num ) {
  32.     int numDigits, digCount;
  33.     UInt32 temp;
  34.     Str32 retVal;
  35.  
  36.     if( num < 0 ) {
  37.         numDigits=1;
  38.         retVal[numDigits] = '-';
  39.         num = -num;
  40.     } else if( num == 0 )
  41.         return "\p0";
  42.     else
  43.         numDigits=0;
  44.     digCount = 0;
  45.     for( temp=1; temp <= num; temp *= 10 )
  46.         digCount++;
  47.     temp /= 10;
  48.     while( digCount-- ) {
  49.         numDigits++;
  50.         retVal[numDigits]=(num/temp)+'0';
  51.         num=num-(num/temp)*temp;
  52.         temp /= 10;
  53.     }
  54.     retVal[0]=numDigits;
  55.     return( retVal );
  56. }
  57.  
  58. unsigned char *StrCat( unsigned char *dest, const unsigned char *src ) {
  59.     int index;
  60.  
  61.     for( index=1; index<=*src; index++ )
  62.         dest[dest[0]+index]=src[index];
  63.     *dest=(*dest)+(*src);
  64.     return( dest );
  65. }
  66.  
  67. unsigned char *StrStr( const unsigned char *src, const unsigned char *sub ) {
  68.     Str255 substr, srcstr;
  69.     int i;
  70.  
  71.     if( sub[0] > src[0] )
  72.         return 0;
  73.     StrCpy( substr, sub );
  74.     StrCpy( srcstr, src );
  75.     for( i = 0; i < src[0]-sub[0]; i++ ) {
  76.         srcstr[i] = sub[0];
  77.         if( !StrCmp( srcstr, substr ) )
  78.             return (unsigned char*)src+i;
  79.     }
  80.     return 0;
  81. }